﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Reflection;

namespace PS.WebParts.Rollup.ResourceTemplates
{
    public class TemplateManager
    {
        public TemplateManager()
        {

        }

        /// <summary>
        /// Process each precompiled template.
        /// </summary>
        /// <param name="path"></param>
        public void CreateFiles(string path)
        {
            path += @"\1214\"; // 12/14 hive

            Dictionary<string, Type> templates = new Dictionary<string, Type>();

            string webPartPath = path + @"TEMPLATE\Features\RollupWebParts\";

            templates.Add(path + @"TEMPLATE\Layouts\ps\rollup\rollupds.ashx", typeof(RollupDSTemplate));

            //The templates have been replaced with WSPBuilder Replacement Parameters
            templates.Add(webPartPath + "PS.WebParts.Calendar.dwp", typeof(CalendarWebPartTemplate));
            templates.Add(webPartPath + "PS.WebParts.ExcelExport.dwp", typeof(ExcelExportWebPartTemplate));
            templates.Add(webPartPath + "PS.WebParts.GridView.dwp", typeof(GridViewWebPartTemplate));
            templates.Add(webPartPath + "PS.WebParts.RollUpDataFormWebPart.dwp", typeof(RollUpDataFormWebPartTemplate));
                        
            string controlTemplatePath = path + @"TEMPLATE\ControlTemplates\";

            templates.Add(controlTemplatePath + "CustomCalendar.ascx", typeof(CustomCalendarControlTemplate));
                        
            foreach(var kv in templates)
            {
                // Need to use reflection as there is no common base class or interface
                // bad microsoft
                object oo = Activator.CreateInstance(kv.Value);

                string content = kv.Value.InvokeMember("TransformText", BindingFlags.InvokeMethod,
                                        Type.DefaultBinder, oo, null).ToString();                                        

                string destination = kv.Key;

                if (File.Exists(destination))
                {//TODO: We should overwrite intead of deleting the file
                    File.Delete(destination);                                        
                }
                                
                File.WriteAllText(destination, content);
            }                        
        }

        public static string GetSharePointFullAssemblyName()
        {
            // This Method will load the assembly from the gac, if the developer has both assemblies installed
            // Could return incorrect assembly version
            //return typeof(Microsoft.SharePoint.DefaultItemOpen).Assembly.FullName;

            DirectoryInfo dir = new DirectoryInfo( Environment.CurrentDirectory);            

            string filePath = dir.FullName + @"\Microsoft.SharePoint.dll";

            string fullName = Assembly.LoadFrom(filePath).FullName;

#if v14
//TODO: Figure out why the incorrect assembly is loaded
            string versionCheck = "ersion=14";
            string opposite = "ersion=12";
#else 
            string versionCheck = "ersion=12";
            string opposite = "ersion=14";
#endif

            if (fullName.Contains(opposite))
                fullName = fullName.Replace(opposite, versionCheck);

            return fullName;
        }
    }
}